﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYue.Permission.BLL;
using XinYue.Permission.Mode;
using XinYue.Permission.Web.Models;

namespace XinYue.Permission.Web.Controllers
{
    public class FunctionController : Controller
    {
        FunctionService functionService = new FunctionService();
        RoleFunctionPermissionService permissionService = new RoleFunctionPermissionService();
        #region 功能管理-查询
        public JsonResult GetListFumctionInfo(int page, int limit, string name)
        {
            //Expression<Func<T, bool>> Wherelambda, Expression< Func < T, S >> OrderBylambda,
            int count = 0;
            //where条件
            Expression<Func<Function, bool>> Wherelambda = a => true;
            if (name != null)
            {
                Wherelambda = a => a.Name.Contains(name);
            }
            Expression<Func<Function, int>> OrderBylambda = a => a.Id;
            PageListResult<Function> pageListResult = new PageListResult<Function>();
            List<Function> list = functionService.QueryPageList(page, limit, Wherelambda, OrderBylambda, out count);
            pageListResult.count = count;
            pageListResult.msg = string.Empty;
            pageListResult.code = 0;
            pageListResult.data = list.Skip((page - 1) * 10).Take(limit).ToList();
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 功能管理-添加/修改
        public JsonResult AddFumctionInfo(Function function)
        {
            Operate operate = new Operate();
            if (function.Id != 0)
            {
                //修改
                operate.Success = functionService.Update(function);
            }
            else
            {
                //添加
                operate.Success = functionService.Add(function);
            }
            return Json(operate);
        }
        #endregion

        #region 功能管理-唯一
        public JsonResult GetFumonInfoOnely(Function function)
        {
            Operate operate = new Operate();
            //where条件
            Expression<Func<Function, bool>> Wherelambda = a => a.MenuId == function.MenuId && a.Name == function.Name;
            operate.Success = functionService.Exists(Wherelambda);
            return Json(operate);
        }
        #endregion

        #region 功能管理-有关联不能删
        public JsonResult FumonInfoRelation(Function function)
        {
            Operate operate = new Operate();
            //where条件
            Expression<Func<RoleFunctionPermission, bool>> Wherelambda = a => a.FunctionId == function.Id;
            operate.Success = permissionService.Exists(Wherelambda);
            return Json(operate);
        }
        #endregion

        #region 功能管理-删除
        public JsonResult DelFumctionInfo(Function function)
        {
            Operate operate = new Operate();
            operate.Success = functionService.Delete(function);
            return Json(operate);
        }
        #endregion

    }
}